package asteroids.model;

import java.util.List;

import asteroids.CollisionListener;
import asteroids.Util;

public class CollisionObject {
	
	public CollisionObject(SpaceObject object1, SpaceObject object2){
		if(object1 == null)
			throw new IllegalArgumentException();
		this.object1=object1;
		this.object2=object2;
	}
	
	public SpaceObject getSpaceObject1(){
		return object1;
	}
	
	public SpaceObject getSpaceObject2(){
		return object2;
	}
	
	private final SpaceObject object1;
	private final SpaceObject object2;
	
	public static void resolveCollision(List<CollisionObject> array, CollisionListener collisionListener) throws NullPointerException{
		for(CollisionObject collisionObject: array){
			 SpaceObject object1 = collisionObject.getSpaceObject1();
			 SpaceObject object2 = collisionObject.getSpaceObject2();
			 if(object1 == null)
				 throw new NullPointerException();
			 if(object2 == null){
				 boundryCollision(object1);
			 }
			 else{
				 double x = object1.getCollisionPosition(object1, object2).getX();
				 double y = object1.getCollisionPosition(object1, object2).getY();
				 if(collisionListener != null)
					 collisionListener.objectCollision(object1, object2, x, y);
				 resolveCollision(object1,object2);
			 }
		}
	}
	
	 public static void resolveCollision(SpaceObject object1, SpaceObject object2){
		 if(object1 == null || object2 == null)
		      throw new NullPointerException();
	 	 if((object1.isShip() && object2.isShip()) || (object1.isAsteroid() && object2.isAsteroid())) {
	 		 SpaceObject.bounceCollide(object1, object2);
	 	 }
	 	 else if(object1.isBullet()){
	 		 if(object2.isShip() && ((asteroids.model.Bullet)object1).getShipsSource() == (asteroids.model.Ship)object2){}
	 	     else{	 
	 			 object1.terminate();
	 			 object2.terminate();
	 	     }
	 	 }
	 	else if(object2.isBullet()){
	 		 if(object1.isShip() && ((asteroids.model.Bullet)object2).getShipsSource() == (asteroids.model.Ship)object1) {}	 			 
	 	    else{	 
	 			 object1.terminate();
	 			 object2.terminate();
	 	    }
	 	 }
	 	else if(object1.isShip() && object2.isAsteroid()){
	 		object1.terminate();
	 	}
	 	else if(object2.isShip() && object1.isAsteroid()){
	 		object2.terminate();
	 	} 
	}
	 
	 private static void boundryCollision(SpaceObject object){
		 double vx = object.getVelocity().getX();
		 double x = object.getPosition().getX() + object.getRadius();
		 if(vx < 0){
		    x = object.getPosition().getX() - object.getRadius();
		 }
		 double vy = object.getVelocity().getY();
		 double y = object.getPosition().getY() + object.getRadius();
		 if(vy < 0){
			    y = object.getPosition().getY() - object.getRadius();
		 }
		 if(Util.fuzzyEquals(x, object.getWorld().getWidth()) || Util.fuzzyEquals(x, 0)){
			 object.setVelocity(Vector.negateX(object.getVelocity()));
		 }
		 if(Util.fuzzyEquals(y, object.getWorld().getHeight()) || Util.fuzzyEquals(y, 0)){
			 object.setVelocity(Vector.negateY(object.getVelocity()));
		 }
		 if(object.isBullet())
			 ((Bullet)object).updateBoundryCounter();
	 }
	

}
